package com.doudou.commons.utils;

import com.doudou.commons.entity.Result;
import lombok.extern.slf4j.Slf4j;

/**
 * @Author: 傻男人
 * @Date: 2020/5/27 9:46
 * @Version: 1.0
 * @Description:
 */
@Slf4j
public class ClientResultUtils {

    /**
     * 远程调用失败返回null
     * @param source
     * @param <T>
     * @return
     */
    public static <T> T getResultIfErrorNull(Result<T> source){
        if(source.getCode() != 0 ){
            return null;
        }
        return source.getData();
    }

    /**
     * 远程调用失败则抛异常
     * @param source
     * @param message 异常的消息
     * @param <T>
     * @return
     */
    public static <T> T getResultIfErrorException(Result<T> source,String message){
        if(source.getCode() != 0 ){
            throw new RuntimeException(message);
        }
        return source.getData();
    }
}
